package com.geeklan.smart.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.geeklan.smart.R;
import com.geeklan.smart.widget.adapter.CalendarAdapter;
import com.geeklan.smart.widget.adapter.CalendarDay;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

public class CalendarView extends FrameLayout {

    private CalendarAdapter calendarAdapter;
    private Calendar currentCalendar;
    private OnDateClickListener onDateClickListener;
    private TextView tvMonth;
    private List<CalendarDay> days = new ArrayList<>();
    public CalendarView(Context context) {
        super(context);
        init(context);
    }

    public CalendarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CalendarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_calendar, this, true);

        tvMonth = findViewById(R.id.currentMonthText);
        RecyclerView rvCalendar = findViewById(R.id.calendarRecyclerView);
        ImageView btnPrev = findViewById(R.id.prevMonthButton);
        ImageView btnNext = findViewById(R.id.nextMonthButton);

        rvCalendar.setLayoutManager(new GridLayoutManager(context, 7));
        calendarAdapter = new CalendarAdapter(context, days, (year, month, day) -> {
            onDateClickListener.onDateClick(year,month,day);
        });
        rvCalendar.setAdapter(calendarAdapter);

        btnPrev.setOnClickListener(v -> changeMonth(-1));
        btnNext.setOnClickListener(v -> changeMonth(1));

        currentCalendar = Calendar.getInstance();
    }

    public void getData(){
        int month = currentCalendar.get(Calendar.MONTH) + 1;
        int year = currentCalendar.get(Calendar.YEAR);
        onDateClickListener.onChangeMonth(year,month);
        updateMonthText();
    }

    private void changeMonth(int delta) {
        currentCalendar.add(Calendar.MONTH, delta);
        getData();
    }

    private void updateMonthText() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月", Locale.getDefault());
        tvMonth.setText(sdf.format(currentCalendar.getTime()));
    }

    public void generateCalendarDays(List<String> date) {
        List<CalendarDay> days = new ArrayList<>();
        Calendar today = Calendar.getInstance();

        int daysInMonth = currentCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        currentCalendar.set(Calendar.DAY_OF_MONTH, 1);
        int firstDayOfWeek = currentCalendar.get(Calendar.DAY_OF_WEEK);
        int month = currentCalendar.get(Calendar.MONTH) + 1;
        int year = currentCalendar.get(Calendar.YEAR);

        // 补全前面的空白日期
        int offset = firstDayOfWeek - Calendar.SUNDAY;
        for (int i = 0; i < offset; i++) {
            days.add(new CalendarDay(year,0, month,false, false,false));
        }

        // 填充日期
        for (int i = 1; i <= daysInMonth; i++) {
            boolean isToday = (today.get(Calendar.YEAR) == currentCalendar.get(Calendar.YEAR) &&
                    today.get(Calendar.MONTH) == currentCalendar.get(Calendar.MONTH) &&
                    today.get(Calendar.DAY_OF_MONTH) == i);
            days.add(new CalendarDay(year,i,month, isToday, true,date.contains(year + "-" + (month < 10 ? "0" + month : month) + "-" + (i < 10 ? "0" + i : i))));
        }

        this.days.clear();
        this.days.addAll(days);
        calendarAdapter.notifyDataSetChanged();
    }

    public void setOnDateClickListener(OnDateClickListener listener) {
        this.onDateClickListener = listener;
    }

    // 日期点击回调接口
    public interface OnDateClickListener {
        void onDateClick(int year,int month,int day);
        void onChangeMonth(int year,int month);
    }
}

